Component({
  properties: {
    // 按钮文字
    text: {
      type: String,
      value: '按钮'
    },
    // 按钮类型
    type: {
      type: String,
      value: 'primary' // primary, success, warning, danger, info, outline, ghost, text, gradient
    },
    // 按钮尺寸
    size: {
      type: String,
      value: 'medium' // mini, small, medium, large, xlarge
    },
    // 按钮形状
    shape: {
      type: String,
      value: 'round' // round, square, circle
    },
    // 左侧图标
    iconLeft: {
      type: String,
      value: ''
    },
    // 右侧图标
    iconRight: {
      type: String,
      value: ''
    },
    // 徽章文字
    badge: {
      type: String,
      value: ''
    },
    // 是否加载中
    loading: {
      type: Boolean,
      value: false
    },
    // 加载文字
    loadingText: {
      type: String,
      value: ''
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      value: false
    },
    // 自定义样式
    customStyle: {
      type: String,
      value: ''
    },
    // 悬浮样式类
    hoverClass: {
      type: String,
      value: 'button-hover'
    },
    // 悬浮开始时间
    hoverStartTime: {
      type: Number,
      value: 20
    },
    // 悬浮持续时间
    hoverStayTime: {
      type: Number,
      value: 70
    },
    // 是否开启触觉反馈
    hapticFeedback: {
      type: Boolean,
      value: true
    },
    // 防抖延迟（毫秒）
    debounceTime: {
      type: Number,
      value: 300
    }
  },

  data: {
    lastClickTime: 0,
    animationClass: ''
  },

  methods: {
    // 按钮点击事件
    onButtonTap(e) {
      // 防抖处理
      const now = Date.now();
      if (now - this.data.lastClickTime < this.data.debounceTime) {
        return;
      }
      this.setData({ lastClickTime: now });

      // 检查状态
      if (this.data.disabled || this.data.loading) {
        return;
      }

      // 触觉反馈
      if (this.data.hapticFeedback) {
        wx.vibrateShort({
          type: 'light'
        });
      }

      // 触发事件
      this.triggerEvent('tap', {
        text: this.data.text,
        type: this.data.type
      });
    },

    // 设置加载状态
    setLoading(loading, text = '') {
      this.setData({
        loading,
        loadingText: text
      });
    },

    // 设置禁用状态
    setDisabled(disabled) {
      this.setData({ disabled });
    },

    // 设置文字
    setText(text) {
      this.setData({ text });
    },

    // 设置类型
    setType(type) {
      this.setData({ type });
    },

    // 设置徽章
    setBadge(badge) {
      this.setData({ badge });
    },

    // 显示成功动画
    showSuccess(callback) {
      this.setData({ animationClass: 'success-animation' });
      setTimeout(() => {
        this.setData({ animationClass: '' });
        if (callback) callback();
      }, 600);
    },

    // 显示错误动画
    showError(callback) {
      this.setData({ animationClass: 'error-animation' });
      setTimeout(() => {
        this.setData({ animationClass: '' });
        if (callback) callback();
      }, 600);
    },

    // 显示震动动画
    shake() {
      this.setData({ animationClass: 'shake' });
      setTimeout(() => {
        this.setData({ animationClass: '' });
      }, 500);
    },

    // 模拟点击
    simulateClick() {
      if (!this.data.disabled && !this.data.loading) {
        this.onButtonTap();
      }
    },

    // 重置按钮状态
    reset() {
      this.setData({
        loading: false,
        disabled: false,
        animationClass: '',
        loadingText: ''
      });
    },

    // 开始加载（带进度模拟）
    startLoading(duration = 2000, callback) {
      this.setLoading(true, '处理中...');
      
      // 模拟进度
      let progress = 0;
      const interval = 100;
      const step = interval / duration * 100;
      
      const timer = setInterval(() => {
        progress += step;
        if (progress >= 100) {
          clearInterval(timer);
          this.setLoading(false);
          this.showSuccess(callback);
        } else {
          this.setData({
            loadingText: `处理中... ${Math.round(progress)}%`
          });
        }
      }, interval);
    },

    // 异步操作包装
    async asyncAction(action, loadingText = '处理中...') {
      try {
        this.setLoading(true, loadingText);
        const result = await action();
        this.setLoading(false);
        this.showSuccess();
        return result;
      } catch (error) {
        this.setLoading(false);
        this.showError();
        throw error;
      }
    }
  }
}); 